﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace NetFlux
{
    public class ConnectionForwarder
    {
        int port;
        string destinationHost;
        int destinationPort;
        string outMsg = "";

        public ConnectionForwarder(string destination, int destinationPort, int localPort)
        {
            this.port = localPort;

            this.destinationHost = destination;
            this.destinationPort = destinationPort;

            outMsg = @"HTTP/1.1 404 Not Found
Content-Length: 964      
Vary: User-Agent
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sat, 14 Dec 2013 12:22:25 GMT
Server: sffe
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 80:quic
Proxy-Connection: keep-alive

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  "+"<meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">"+
  @"<title>Error 404 (Not Found)!!1 NetFlux</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.netflux.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <p><b>404.</b> <ins>That???s an error.</ins>
  <p>The requested URL <code>/personal/main/weboutoffunds/?url=</code> was not found on this server.  <ins>That???s all we know.</ins>
";
        }

        static void WriteOut(string text, NetworkStream stream)
        {
            byte[] data = Encoding.ASCII.GetBytes(text);
            stream.Write(data, 0, data.Length);
        }

        TcpListener server;
        Thread listenThread;
        public void Start()
        {
            try
            {
                
                server = new TcpListener(IPAddress.Any,port);
                server.Start();
            }
            catch
            {
                throw new Exception("Could not open port");
            }

            listenThread = new Thread(() =>
            {
                while (true)
                {
                    TcpClient client = server.AcceptTcpClient();

                    new Thread(() =>
                    {
                        try
                        {
                            
                            NetworkStream clientStream = client.GetStream();
                            byte[] buffer = new byte[8096];
                            int i = clientStream.Read(buffer, 0, buffer.Length);
                            string msg = Encoding.ASCII.GetString(buffer, 0, i);
                            Console.Write("Connection from " + client.Client.RemoteEndPoint.ToString() +" on ");
                            if (msg.Contains("vodacom"))
                                Console.Write("Vodacom");
                            else if(msg.Contains("Telkom"))
                                Console.Write("Telkom");
                            Console.Write(". Client has sent {0} bytes ",i);
                            Console.WriteLine();

                            byte[] vlive = Encoding.ASCII.GetBytes(outMsg);
                            clientStream.Write(vlive, 0, vlive.Length); clientStream.Flush();

                            TcpClient destination = new TcpClient(destinationHost, destinationPort);

                            NetworkStream destinationStream = destination.GetStream();
                            Pwned.Connection connection = new Pwned.Connection(clientStream, destinationStream);
                            connection.Run();
                            destination.Close();
                            client.Close();
                            connection = null;
                            destinationStream = null;
                            clientStream = null;

                        }
                        catch
                        {
                            try
                            {
                                client.Close();
                            }
                            catch { }
                        }
                    }) { IsBackground = true }.Start();
                }

            }) { IsBackground = true };
            listenThread.Start();
        }

        public void Stop()
        {
            try
            {
                server.Stop();
            }
            catch
            {
            }
            try
            {
                listenThread.Abort();
            }
            catch
            {
            }
        }

    }
}
